/**
 * Login Auth
 */

var m_Userkey = null ;
var m_Usernick = null ;

var LoginResult = {
		SUCCESS : 10000 , 
		ERROR   : 20001
		
}



/**
 * login function
 * 
 * @username
 * @password
 * @callback(loginResult[boolean] , errorMsg[String] )
 */
function login(username , password ,callback){
	Ajax.asyncLogin("http://sp.autohome.com.cn/Nokia/Login.ashx" , username , password , 
		function (text){
			var xmlDoc = loadXml(text) ;
			if(xmlDoc == null){
				
			}
			var oNodes = xmlDoc.selectNodes( "//Response/User "); 
			
			callback(true ,text) ;
		} ,		
		function(text){
			text = '<?xml version="1.0"?><Response><User Key="7YHlU4mXx5GbNfUyMhUhoho" Usernick="csjxing" Img=""/></Response>' ;
			var xmlDoc = loadXml(text) ; 
			//Response
			var responseElement = xmlDoc.getElementsByTagName("Response")[0];
			
			
			if(responseElement != null){
				//Success
				
				var firstElement  = responseElement.firstChild ;
				if(firstElement.nodeName == "User"){
					//
					var userkey = firstElement.attributes['Key'] ;
					var nickname = firstElement.attributes['Usernick'] ;
					m_Userkey = userkey ;
					m_Userkey = nickname ;
					callback(true,"Login Success") ;
				}else if(firstElement.nodeName == "Error"){
					var errorMsg = firstElement.attributes['Message'];
					callback(false,errorMsg) ;
				}else{
                                        callback(false , "未知错误");
				}
				
				
			}
                        callback(false,"网络或服务器错误") ;
		}
	)
	
}

/**
 * return xmlDoc
 */
function loadXml(xmlString){
    	var xmlDoc=null;
	   if(!window.DOMParser && window.ActiveXObject){   // window.DOMParser
	        var xmlDomVersions = ['MSXML.2.DOMDocument.6.0','MSXML.2.DOMDocument.3.0','Microsoft.XMLDOM'];
	       for(var i=0;i<xmlDomVersions.length;i++){
	           try{
	                xmlDoc = new ActiveXObject(xmlDomVersions[i]);
	                xmlDoc.async = false;
	                
	                xmlDoc.loadXML(xmlString); // 
	               break;
	            }catch(e){
	            }
	        }
	   }else if(window.DOMParser && document.implementation && document.implementation.createDocument){
	       try{
	            domParser = new   DOMParser();
	            xmlDoc = domParser.parseFromString(xmlString, 'text/xml');
	        }catch(e){
	        }
	    }
	   else{
	       return null;
	    }
	
	   return xmlDoc;
}














/**
 * Ajax tool
 * 
 * @namespace xui.util
 * @singleton Ajax
 */
var Ajax = {
	
	asyncLogin : function(url, user , pass , successFun, failureFun) {
		var http = this._createHTTP();
		http.onreadystatechange = function() {
			if (http.readyState == 4) {
				if (http.status == 200) {
					Ajax._callback(successFun, http.responseText);
				} else {
					Ajax._callback(failureFun, http.responseText);
				}
			}
		};
		// url = Ajax._newURL(url);
		http.open('GET', url , true);
		
		http.setRequestHeader("AuthorizationAutohome", user+":"+pass);
		http.setRequestHeader("cache-control", "no-cache");
		
		http.send('<Request type="Login" phonewidth="320" phoneheight="480" phonetype="N9" />');
	},
	_iframeCallback : {},
	_iframeOnload : function(id) {
		try {
			Ajax._callback(Ajax._iframeCallback[id],
					window.frames[id].document.body.innerHTML);
		} catch (e) {
			alert(e) ;
		}
		;
	},
	
	/**
	 * , syncPost:function(url,data){ var http = this._createHTTP();
	 * http.open('post',xui.util.Ajax._newURL(url),false);
	 * http.setRequestHeader("cache-control","no-cache");
	 * //http.setRequestHeader("If-Modified-Since","0"); http.send(data);
	 * if(http.readyState==4){ if(http.status==200){ return http.responseText; }
	 * else{ return null; } } }
	 */
	_createHTTP : function() {
		var http;
		try {
			http = new XMLHttpRequest();
		} catch (e) {
			try {
				http = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					http = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					http = null;
				}
			}
		}
		return http;
	},
	_newURL : function(url) {
		if (url.indexOf('?') > 0) {
			s = url + '&ts=' + new Date().getTime();
		} else {
			s = url + '?ts=' + new Date().getTime();
		}
		return s;
	},
	_callback : function(oFunction, res) {
		if (oFunction)
			oFunction.call(this, Ajax._trim(res));
	},
	
	_trim : function(str) {
		if (str && str.length > 0) {
			return str.replace(/(^\s*)|(\s*$)/g, '');
		}
		return '';
	}
}
